package com.example.highclasscomponent;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LaunchFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LaunchFragment extends Fragment {

    public static LaunchFragment newInstance(int count,int position, int imageId) {
        LaunchFragment fragment = new LaunchFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putInt("imageId", imageId);
        args.putInt("count",count);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Bundle arguments = getArguments();
        Context context = getContext();//Fragment类中可以直接获取context
        if (arguments == null) {
            return null;
        }
        int position = arguments.getInt("position");
        int imageId = arguments.getInt("imageId");
        int count=arguments.getInt("count");
        View view = inflater.inflate(R.layout.fragment_launch, container, false);
        ImageView iv_launch = view.findViewById(R.id.iv_launch);
        RadioGroup rg_indicate = view.findViewById(R.id.rg_indicate);
        Button btn_start = view.findViewById(R.id.btn_start);
        iv_launch.setImageResource(imageId);
        for (int i = 0; i < count; i++) {
            RadioButton rdb=new RadioButton(context);
            rdb.setPadding(10,10,10,10);
            rdb.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            rg_indicate.addView(rdb);
        }
        ((RadioButton)rg_indicate.getChildAt(position)).setChecked(true);
        if(position==count-1)
        {
            btn_start.setVisibility(View.VISIBLE);
            btn_start.setOnClickListener(v->{
                Toast.makeText(context, "立刻开始美好生活", Toast.LENGTH_SHORT).show();
            });
        }
        return view;
    }
}